/*******************************************************************

   MenuWindow.c
	
	Does open a simple window with some easy gadgets and a very
	simple menu.
		
			
*********************************************************************/

#include "MenuWindow.h"

/********************************************************************/
// locale prototypes

BOOL OpenDOpusWin( WindowHandle *wh );

BOOL HandleWindow( WindowHandle *wh );

BOOL HandleMenu( WindowHandle *wh ); 

/********************************************************************/

void OwnWindow( STRPTR args, struct Screen *screen )
{
	WindowHandle          *wh;
	
	if( (wh = AllocMemH(mempool, sizeof(WindowHandle))) ) // allocate some memory
	  {
		  wh->screen = screen;     // store the screen pointer
		  
		  if( OpenDOpusWin(wh) )   // open the window
	       {
				 // we copy now the arguments into the text gadget
		       // if we have supplied some...
				 
		       SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) args );
				 				 				 
		       while( TRUE ) 
			      {
						// I use here Wait(), so it is easier with the changes
				 
				      wh->signals = Wait( 1 << wh->win->UserPort->mp_SigBit );   // wait for window events
						                				
				      if( wh->signals & 1 << wh->win->UserPort->mp_SigBit )
					        if( HandleWindow(wh) )
							       break;		// does end the while loop									   
					 
			      }
			 
		       CloseConfigWindow( wh->win ); 
			 }
			 
		  FreeMemH( wh ); // free our memory
	  }	  					  
}

/********************************************************************/
// This function does open our window. We could have done this in the
// function OwnWindow() too, but we need this function later again.

BOOL OpenDOpusWin( WindowHandle *wh )
{
	NewConfigWindow ncfgwin;     // we need a NewConfigWindow structure too
	                             // of couse you could also allocate it with
										  // AllocMemH()...
	
	// and have to fill it
	
	ncfgwin.nw_Parent = wh->screen;  // open on this screen
	
	// getting a localized title...
	ncfgwin.nw_Title  = DOpusGetString( locale, MSG_WINDOW_TITLE );
	
	ncfgwin.nw_Dims   = &cfgwin; // a pointer to the ConfigWin structure
	ncfgwin.nw_Locale = locale;  // the module locale pointer (from modinit.c)
	ncfgwin.nw_Port   = NULL;    // we doesn't supply a port
	ncfgwin.nw_Font   = NULL;    // just taking the screen font
	ncfgwin.nw_Flags  = WINDOW_REQ_FILL      | // fill with stripple pattern
	                    WINDOW_AUTO_KEYS     | // handle keys automatic
							  WINDOW_SCREEN_PARENT;  // nw_Parent points to a screen
	
	if( (wh->win = OpenConfigWindow(&ncfgwin)) )   // open the window
	  {
		  if( (wh->olist = AddObjectList(wh->win, odef)) ) // add the gadgets
		    {
				 // now we add the menu...
				 
				 AddWindowMenus( wh->win, winmenu );
				 
		     	 return TRUE;
			 }
					
		  CloseConfigWindow( wh->win );	//	in error case do not forget :-)
	  }
	
	return FALSE;
}


/********************************************************************/
// we does only close the window, if the closegadget was pressed
// if you want to close it within a gadget, you must only in the
// right case set "stop" to TRUE

BOOL HandleWindow( WindowHandle *wh )
{
	 BOOL stop = FALSE;
	 ULONG value;
	 
	 while( !stop && (wh->imsg = GetWindowMsg(wh->win->UserPort)) )
		{
			switch( wh->imsg->Class )  // let's handle the IDCMP
			  {
				  case IDCMP_GADGETUP:
				                          switch( GET_ID(wh->imsg) )
	                                     {
														 case GADGET_ID_CYCLE:  // we copy simply the same text to the text gadget
			
			                                                         value = GetGadgetValue( wh->olist, GADGET_ID_CYCLE ) + MSG_CLICK_ME;
										                                    SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, value) );
										                                    break;
			
			                                  case GADGET_ID_OKAY:   // doing a message
			                                                         
																						SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_OKAY_DONE) );
										                                    break;
										 			
			                                  case GADGET_ID_CANCEL: 
			                                                         SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_CANCEL_DONE) );
										                                    break;
		                                  }
																 
												  break;
									 
				  case IDCMP_CLOSEWINDOW:
				                          // we can not simply return here, the IntuiMessage must replied first
												  
									           stop = TRUE;
												  break;		
												  
				  case IDCMP_MENUPICK:
				                          stop = HandleMenu( wh );
												  break;							 
			  }
								 
			ReplyWindowMsg( wh->imsg );  
							  
			// remember: You should not use any other routines
			// to get/reply the messages of this window than
			// GetWindowMsg() and ReplyWindowMsg() !!
	   }
						  	 
    return stop;
}

/********************************************************************/

BOOL HandleMenu( WindowHandle *wh )
{
     switch( GET_MENUID(wh->win, wh->imsg) )
	    {
			 case MENU_ID_SAVE: // doing a simple thing
			                    
									  DisplayBeep(NULL);			  
			                    break;
									  
			 case MENU_ID_QUIT: // we does really quit here
			 
			                    return TRUE;
														  
			 case MENU_ID_ITEM: // I leave it here empty
			                    // it should be no problem for you to
									  // insert something... 
			                    
			                    break;
	    }
		 
	  return FALSE;
}